package com.stn.zjx.givingtreasure.activity.bi;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.utils.StatusBarUtil;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxImageTool;
import com.vondear.rxtools.view.RxQRCode;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class BReceivableCodeActivity extends BaseActivity {

    private static final int PRC_PHOTO_PICKER = 1010;
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.share_bt)
    TextView shareBt;
    @BindView(R.id.avatar_civ)
    CircleImageView avatarCiv;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.address_iv)
    ImageView addressIv;
    @BindView(R.id.copy_bt)
    ButtonBgUi copyBt;
    @BindView(R.id.rootView)
    NestedScrollView rootView;
    private String address = "";

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        address = getIntent().getStringExtra("address");
        addressTv.setText(address);
        copyBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ToolsUtils.copyKeybroadContent(BReceivableCodeActivity.this, address)) {
                    showToast(getString(R.string.already_copy_content));
                } else {
                    showToast(getString(R.string.db_error));
                }
            }
        });
        Bitmap bitmap = RxQRCode.creatQRCode(address);
        addressIv.setImageBitmap(bitmap);
        shareBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap bitmap = ToolsUtils.getBitmapByView(rootView);
                savetoImg(bitmap);
            }
        });
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receivable_code);
        StatusBarUtil.setImmersiveStatusBar(this,false);
        ButterKnife.bind(this);
        initComponent();
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @OnClick({R.id.back_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;

        }
    }

    @AfterPermissionGranted(PRC_PHOTO_PICKER)
    public void savetoImg(Bitmap bitmap) {
        String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
        if (EasyPermissions.hasPermissions(this, perms)) {
            showLoading();
            // 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
            File file = new File(Environment.getExternalStorageDirectory().getPath(), getString(R.string.app_name));
            if (!file.exists()) {   //判断文件夹是否存在，不存在则创建
                file.mkdirs();
            }
            String fileName = "";
            fileName = UUID.randomUUID().toString().substring(0, 4) + ".jpg";
            String com_file = file.getPath() + "/" + fileName;
            boolean isSuccess = RxImageTool.save(bitmap, com_file, Bitmap.CompressFormat.JPEG);
            if (isSuccess) {
                // 把文件插入到系统图库
                try {
                    String s = MediaStore.Images.Media.insertImage(getContentResolver(), com_file, fileName, "分享图片");

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + com_file));
                // 通知图库更新
                sendBroadcast(intent);

                ToolsUtils.shareImage(this, new File(com_file));
            } else {
                showToast("save error");
            }
            dissmissLoading();
        } else {
            EasyPermissions.requestPermissions(this, getString(R.string.request_perssion_desc), PRC_PHOTO_PICKER, perms);
        }
    }

}
